<?php

namespace app\api\controller\app;

use app\admin\model\Banner;
use app\admin\model\City;
use app\admin\model\Org;
use app\admin\model\OrgIndexNotice;
use app\admin\model\OrgNotice;
use app\admin\model\Roomclean;
use app\admin\model\Shop;
use app\admin\model\Shopdevice;
use app\admin\model\Shopmno;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproom;
use app\admin\model\Shoproombook;
use app\admin\model\Userbalance;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\api\service\DeviceService;
use app\api\service\MtService;
use app\api\service\MtUtil;
use app\api\service\VbillService;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Http;
use fast\Random;
use think\Config;
use think\Validate;

/**
 */
class Sync extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

    }

    public function test()
    {

        $order = '{"shop_id":"664","shop_name":"\u957f\u6c99-\u96c0\u4fa0\u603b\u90e8\u5e97(\u6f14\u793a\u7528)","room_id":"23174","room_name":"\u6b66\u5f53","user_id":1,"order_no":"25022714281198525648674875","create_time":1740637691,"update_time":1740637725,"amount":"0.01","pay_amount":"0.01","status":"payed","book_start":1740723780,"book_end":1740727380,"pay_config_id":null,"set_id":6959,"balance":"0.00","open_code":"","tuan_id":-1,"pay_rate":"0.00","rate":"0.00","refund_order_no":"","mt_code":"","org_id":27,"dou_code":"","dou_token":"","score":0,"is_delete":-1,"coupon_id":-1,"xu_order_no":"","refund_amount":"0.00","is_check":-1,"from":"","is_comment":-1,"comment_id":-1,"orgin_order_no":"","ks_code":"","ks_verify_token":"","mark":"","balance_type":0,"mt_trace_id":"","create_time_text":"2025-02-27 14:28:11","update_time_text":"2025-02-27 14:28:45","pay_type_text":"","status_text":"\u5f85\u6d88\u8d39"}';

        $order = json_decode($order,true);
        $shopOrderModel = new Shoporder();
        $order_id = $shopOrderModel->insertGetId($order);
        $this->success('',$order_id);
    }

    public function order()
    {

        $shop_id = $this->request->param('shop_id');
        $room_id = $this->request->param('room_id');
        $mobile = $this->request->param('mobile');
        $order = $this->request->param('order');
        $book = $this->request->param('book');
        $clean = $this->request->param('clean');

        $userModel = new \app\admin\model\User();
        $user = $userModel->where('mobile',$mobile)->find();
        if(!$user){
            $db = array();
            $db['group_id'] = 1;
            $db['username'] = "微信用户";
            $db['mobile'] = $mobile;
            $db['createtime'] = time();
            $db['status'] = 'normal';
            $db['org_id'] = 27;
            $user_id = $userModel->insertGetId($db);
        }else{
            $user_id = $user['id'];
        }

        $order = json_decode(htmlspecialchars_decode($order),true);
        $book = json_decode(htmlspecialchars_decode($book),true);
        $clean = json_decode(htmlspecialchars_decode($clean),true);

        unset($order['id']);
        unset($order['create_time_text']);
        unset($order['update_time_text']);
        unset($order['status_text']);
        unset($order['pay_type_text']);

        $order['create_time'] = time();
        $order['room_id'] = $room_id;
        $order['shop_id'] = $shop_id;
        $order['user_id']  = $user_id;
        $order['is_third'] = 1;

        $shopOrderModel = new Shoporder();
        $order_id = $shopOrderModel->insertGetId($order);

        unset($book['id']);
        unset($book['start_time_text']);
        unset($book['end_time_text']);

        $book['room_id'] = $room_id;
        $book['shop_id'] = $shop_id;
        $book['user_id']  = $user_id;


        $bookModel = new Shoproombook();
        $bookModel->insert($book);

        unset($clean['id']);
        unset($clean['start_time_text']);
        unset($clean['end_time_text']);
        unset($clean['status_text']);


        $clean['order_id'] = $order_id;
        $clean['room_id'] = $room_id;
        $clean['shop_id'] = $shop_id;
        $roomCleanModel = new Roomclean();
        $roomCleanModel->insert($clean);
        $this->success($mobile.'-'.$room_id,$order);
    }

}
